package com.stgl.action;

import com.stgl.service.TeacherService;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@EnableAutoConfiguration
@RequestMapping("/teacher")
public class teacher {
    @Resource
    private TeacherService teacherService;
    @RequestMapping("Search_Teacher.html")
    @ResponseBody
    public Map<String,Object>Search_Teacher(HttpServletRequest request, HttpServletResponse response){
        String tname=request.getParameter("tname").trim();
        Map<String,Object>gmap=new HashMap<String,Object>();
        gmap.put("tname",tname);

        HttpSession session=((HttpServletRequest)request).getSession();
        if(session.getAttribute("csid")!=null){
            gmap.put("csid",session.getAttribute("csid").toString());
        }

        List<Map<String,Object>>list1=teacherService.Search_Teacher(gmap);
        Map<String,Object>rmap=new HashMap<String,Object>();
        rmap.put("data",list1);
        return rmap;
    }


    @RequestMapping("del_Teacher.html")
    @ResponseBody
    public void del_Teacher(HttpServletRequest request, HttpServletResponse response){
        String tid=request.getParameter("tid").trim();
        Map<String,Object>gmap=new HashMap<String,Object>();
        gmap.put("tid",tid);
        teacherService.del_Teacher(gmap);
    }


    @RequestMapping("Insert_Teacher.html")
    @ResponseBody
    public Boolean Insert_Teacher(HttpServletRequest request, HttpServletResponse response){
        String tname=request.getParameter("tname").trim();
        String jianjie=request.getParameter("jianjie").trim();
        String photo=request.getParameter("photo").trim();

        Map<String,Object>gmap=new HashMap<String,Object>();
        HttpSession session=((HttpServletRequest)request).getSession();
        if(session.getAttribute("csid")!=null){
            gmap.put("csid",session.getAttribute("csid").toString());
        }else{
            return false;
        }
        gmap.put("tname",tname);
        gmap.put("jianjie",jianjie);
        gmap.put("photo",photo);
        gmap.put("tstate",0);
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        gmap.put("tid",uuid);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        gmap.put("addtime",df.format(new Date()));

        int i=teacherService.Insert_Teacher(gmap);
        if(i>=0){
            return true;
        }
        return false;
    }


    @RequestMapping("Edit_Teacher.html")
    @ResponseBody
    public Boolean Edit_Teacher(HttpServletRequest request, HttpServletResponse response){
        String tid=request.getParameter("tid").trim();
        String tname=request.getParameter("tname").trim();
        String jianjie=request.getParameter("jianjie").trim();
        String photo=request.getParameter("photo").trim();

        Map<String,Object>gmap=new HashMap<String,Object>();
        gmap.put("tid",tid);
        gmap.put("tname",tname);
        gmap.put("jianjie",jianjie);
        gmap.put("photo",photo);

        int i=teacherService.Edit_Teacher(gmap);
        if(i>=0){
            return true;
        }
        return false;
    }


    @RequestMapping("GetTeacherByTname.html")
    @ResponseBody
    public List<Map<String,Object>>GetTeacherByTname(HttpServletRequest request, HttpServletResponse response){
        String tname=request.getParameter("tname").trim();
        Map<String,Object>gmap=new HashMap<String,Object>();
        gmap.put("tname",tname);
        List<Map<String,Object>>list1=teacherService.GetTeacherByTname(gmap);
        return list1;
    }



}
